/**
 *  Copyright 2002-2009 the original author or authors.
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */
package foo.bar.blog.web.action.registered;

import foo.bar.blog.core.domain.*;
import foo.bar.blog.core.services.BlogService;
import foo.bar.blog.core.services.exception.BusinessLogicExecution;
import foo.bar.blog.radeox.DecoratedCommentTransformer;
import foo.bar.blog.radeox.BlogRenderEngine;
import foo.bar.radeox.api.engine.RenderEngine;
import foo.bar.radeox.engine.context.BaseRenderContext;
import foo.bar.blog.utils.CollectionUtils;
import foo.bar.blog.utils.IOUtils;
import foo.bar.blog.web.action.ExtendedPaginatedList;
import foo.bar.blog.web.action.PaginatedBaseActionBean;
import foo.bar.blog.web.extension.*;
import foo.bar.blog.web.nonextension.HtmlResolution;
import foo.bar.blog.web.nonextension.NullResolution;
import net.sourceforge.stripes.action.*;
import net.sourceforge.stripes.controller.LifecycleStage;
import net.sourceforge.stripes.integration.spring.SpringBean;
import net.sourceforge.stripes.validation.*;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.perf4j.aop.Profiled;
import org.jsecurity.SecurityUtils;

import java.io.IOException;
import java.net.URISyntaxException;
import java.util.List;
import java.util.Set;
import java.util.LinkedHashMap;

/**
 * Blog profile related actions.
 *
 * @author tmjee
 * @version $Date: 2009-07-12 15:39:06 +0000 (Sun, 12 Jul 2009) $ $Id$
 */
public class BlogsProfileActionBean extends PaginatedBaseActionBean {

    private static final Log LOG = LogFactory.getLog(BlogsProfileActionBean.class);


    public static final String VIEW = "registered/blogs.jsp";
    public static final String VIEW_BLOG_DETAILS = "registered/blog_view_details.jsp";
    public static final String EDIT_BLOG_DETAILS = "registered/blog_edit_details.jsp";
    public static final String NEW_BLOG = "registered/blog_new.jsp";
    public static final String VIEW_BLOG_ENTRIES = "registered/blog_entries.jsp";
    public static final String NEW_BLOG_ENTRY = "registered/blog_new_entry.jsp";
    public static final String VIEW_BLOG_ENTRY = "registered/blog_view_entry_details.jsp";
    public static final String EDIT_BLOG_ENTRY = "registered/blog_edit_entry_details.jsp";
    public static final String VIEW_BLOG_ENTRY_COMMENTS = "registered/blog_view_entry_comment_details.jsp";
    public static final String VIEW_LINKS="registered/view_links.jsp";
    public static final String VIEW_LINKS_TABLE = "registered/view_links_table.jsp";
    public static final String ADD_LINK = "registered/add_link.jsp";
    public static final String EDIT_LINK = "registered/edit_link.jsp";
    public static final String VIEW_TAGS = "registered/view_tags.jsp";
    public static final String VIEW_TAGS_TABLE = "registered/view_tags_table.jsp";
    public static final String ADD_TAG = "registered/add_tag.jsp";
    public static final String EDIT_TAG = "registered/edit_tag.jsp";
    public static final String VIEW_RESOURCES = "registered/view_resources.jsp";
    public static final String VIEW_RESOURCES_TABLE = "registered/view_resources_table.jsp";
    public static final String ADD_RESOURCE = "registered/add_resource.jsp";
    public static final String EDIT_RESOURCE = "registered/edit_resource.jsp";
    public static final String MEMBERSHIP = "registered/membership.jsp";
    public static final String NON_MEMBERSHIP_TABLE = "registered/non_membership_table.jsp";
    public static final String MEMBERSHIP_TABLE = "registered/membership_table.jsp";


    private ExtendedPaginatedList ownershipBlogs;
    private ExtendedPaginatedList membershipBlogs;
    private ExtendedPaginatedList allBlogs;

    private ExtendedPaginatedList blogEntries;

    private ExtendedPaginatedList links;
    private ExtendedPaginatedList tags;
    private ExtendedPaginatedList resources;

    @Validate(converter = LinkTypeConverter.class, on={"saveLink","editLink","updateLink"})
    @ValidateNestedProperties({
        @Validate(field="name",trim=true,required=true,on={"saveLink","updateLink"}),
        @Validate(field="description",trim=true,required=true,on={"saveLink","updateLink"}),
        @Validate(field="link",trim=true,required=true,on={"saveLink","updateLink"})
    })
    private Link link;

    @Validate(converter= TagTypeConverter.class, on={"saveTag","editTag","updateTag"})
    @ValidateNestedProperties({
        @Validate(field="name",trim=true,required=true,on={"saveTag","updateTag"}),
        @Validate(field="description",trim=true,required=true,on={"saveTag","updateTag"})
    })
    private Tag tag;

    @Validate(converter= ResourceTypeConverter.class, on={"saveResource","editResource","updateResource"})
    @ValidateNestedProperties({
        @Validate(field="name",trim=true,required=true,on={"saveResource","updateResource"}),
        @Validate(field="description",trim=true,required=true,on={"saveResource","updateResource"})
    })                                                                         
    private Resource resource;

    @Validate(required = true, on={"saveResource", "updateResource"})
    private FileBean resourceFile;


    private Set blogEntryComments;

    @Validate(converter= EntryTypeConverter.class, on={"viewBlogEntry","editBlogEntry","deleteBlogEntry","saveBlogEntry","publishBlogEntry", "previewBlogEntry"})
    @ValidateNestedProperties({
        @Validate(field="blog", trim=true, required=true,  converter = BlogTypeConverter.class, on={"saveBlogEntry"}),
        @Validate(field="name",trim=true, required=true, on={"saveBlogEntry","publishBlogEntry"}),
        @Validate(field="description", trim=true, required=true, on={"saveBlogEntry","publishBlogEntry"}),
        @Validate(field="content", trim=true, required=true, on={"saveBlogEntry","publishBlogEntry"}),
        @Validate(field="tag", converter = TagTypeConverter.class, on={"saveBlogEntry","publishBlogEntry"}),
        @Validate(field="commentExpiryDate", trim=true, converter= DateTypeConverter.class, on={"saveBlogEntry", "publishBlogEntry"})
    })
    private Entry blogEntry;


    @Validate(converter= CommentTypeConverter.class, on={"deleteBlogEntryComments"})
    private Comment blogEntryComment;


    private String previewContent;

    private BlogService blogService;
    private RenderEngine renderEngine;

    private List<String> allBlogThemeNames;


    @Validate(converter = BlogTypeConverter.class, on = {"editBlog","deleteBlog","viewBlog","saveBlog","updateBlog","membership","membershipSearch","membershipAdd","membershipDelete"})
    @ValidateNestedProperties({
        @Validate(field="owner", converter = UserTypeConverter.class, trim=true, required=true, on = {"saveBlog"}),
        @Validate(field="name", trim=true, required = true, on = {"saveBlog"}),
        @Validate(field="description", trim=true,required=true, on = {"saveBlog","updateBlog"}),
        @Validate(field="blogThemeName", trim=true, required=true, on={"saveBlog","updateBlog"})
    })
    private Blog blog;


    private ExtendedPaginatedList entries;


    /**
     * Display the all blogs (blogs the user has ownership of and is member of if he doesn't have permission to
     * view all blogs).
     * 
     * Input - none
     * Output - ownershipBlogs, membershipBlogs OR allBlogs
     * @return
     */
    @DefaultHandler
    @Profiled(tag="BlogsProfileActionBean_view", logFailuresSeparately = true)
    public Resolution view() {
        if (SecurityUtils.getSubject().isPermitted("blog:viewBlogs:*")) {
            ExtendedPaginatedList allBlogsPaginatedList = getPaginatedList("allBlogsPaginatedList");

            allBlogs = blogService.getAllBlogs(allBlogsPaginatedList);
        }
        else {
            ExtendedPaginatedList ownershipBlogsPaginatedList = getPaginatedList("ownershipBlogsPaginatedList");
            ExtendedPaginatedList membershipBlogsPaginatedList = getPaginatedList("membershipBlogsPaginatedList");

            ownershipBlogs = blogService.getOwnershipBlogs(getCurrentUser(), ownershipBlogsPaginatedList);
            membershipBlogs = blogService.getMembershipBlogs(getCurrentUser(), membershipBlogsPaginatedList);
        }
        return new ForwardResolution(generateThemePath(VIEW));
    }


    /**
     * (Ajax) display portion of the page containing a specific blog details (viewable only).
     * Input - blog
     * Output - blog
     * @return
     */
    @Profiled(tag="BlogsProfileActionBean_viewBlog", logFailuresSeparately = true)
    public Resolution viewBlog() {
        return new ForwardResolution(generateThemePath(VIEW_BLOG_DETAILS));        
    }

    /**
     * (Ajax) display portion of the page containinig a specific blog details (editable)
     * Input - blog
     * Output - blog
     * @return
     */
    @Profiled(tag="RegisterActionBean_editBlog", logFailuresSeparately = true)
    public Resolution editBlog() {
        return new ForwardResolution(generateThemePath(EDIT_BLOG_DETAILS));
    }


    /**
     * delete a blog, redirects to view all blogs profile
     * Input - blog
     * Output - none
     * @return
     */
    @Profiled(tag="BlogsProfileActionBean_deleteBlog", logFailuresSeparately = true)
    public Resolution deleteBlog() {
        blogService.deleteBlog(blog);
        getContext().getMessages().add(new LocalizableMessage("foo.bar.blog.web.action.registered.BlogsProfileActionBean.messages.blogDeleted"));
        return new RedirectResolution(BlogsProfileActionBean.class, "view");
    }


    /**
     * (Ajax) Display portion of the page with form for new blog creation.
     * Input - none
     * Output - none
     * @return
     */
    @Profiled(tag="BlogsProfileActionBean_newBlog", logFailuresSeparately = true)
    public Resolution newBlog() {
        return new ForwardResolution(generateThemePath(NEW_BLOG));
    }


    /**
     * Save blog, redirect to edit of that blog
     * Input - blog
     * Output - blog
     * @return
     * @throws Throwable
     */
    @Profiled(tag="BlogsProfileActionBean_saveBlog", logFailuresSeparately = true)
    public Resolution saveBlog() throws Throwable {
        return handleBusinessLogicException(new BusinessLogicExecution(){
            public Resolution execute() throws Throwable {
                blogService.saveBlog(blog);
                getContext().getMessages().add(new LocalizableMessage("foo.bar.blog.web.action.registered.BlogsProfileActionBean.messages.blogSaved"));
                return new RedirectResolution(BlogsProfileActionBean.class, "editBlog")
                            .addParameter("blog", blog.getId());
            }
        });
    }


    /**
     * Update blog profile info.
     * Input - blog
     * Output - blog
     * @return
     */
    @Profiled(tag="BlogsProfileActionBean_updateBlog", logFailuresSeparately = true)
    public Resolution updateBlog() {
        blogService.updateBlog(blog);
        getContext().getMessages().add(new LocalizableMessage("foo.bar.blog.web.action.registered.BlogsProfileActionBean.messages.blogSaved"));
        return new RedirectResolution(BlogsProfileActionBean.class, "editBlog")
                    .addParameter("blog", blog.getId());
    }



    /**
     * Display the blog entries profile.
     * Input - blog
     * Putput - blogEntries, blog
     * @return
     */
    @Profiled(tag="BlogsProfileActionBean_viewBlogEntries", logFailuresSeparately = true)
    public Resolution viewBlogEntries() {
        blogEntries = blogService.getEntries(blog, getPaginatedList());
        return new ForwardResolution(generateThemePath(VIEW_BLOG_ENTRIES));
    }


    /**
     * Load the page for blog entry creation.
     * Input - blog
     * Output - blog
     * @return
     */
    @Profiled(tag="BlogsProfileActionBean_newBlogEntry", logFailuresSeparately = true)
    public Resolution newBlogEntry() {
        return new ForwardResolution(generateThemePath(NEW_BLOG_ENTRY));
    }


    /**
     * Save the blog entry
     * Input - blogEntry
     * Output - blog, blogEntry
     * @return
     * @throws Throwable
     */
    @Profiled(tag="BlogsProfileActionBean_saveBlogEntry", logFailuresSeparately = true)
    public Resolution saveBlogEntry() throws Throwable {
        return handleBusinessLogicException(new BusinessLogicExecution(){
            public Resolution execute() throws Throwable {
                blogService.saveBlogEntry(blogEntry);
                getContext().getMessages().add(new LocalizableMessage("foo.bar.blog.web.action.registered.BlogsProfileActionBean.messages.blogEntrySaved"));
                return new RedirectResolution(BlogsProfileActionBean.class, "editBlogEntry")
                        .addParameter("blog", blogEntry.getBlog().getId())
                        .addParameter("blogEntry", blogEntry.getId());
            }
        });
    }


    /**
     * Publish blog entry
     * Input - blogEntry
     * Output - blog, blogEntry
     * @return
     * @throws Throwable
     */
    @Profiled(tag="BlogsProfileActionBean_publishBlogEntry", logFailuresSeparately = true)
    public Resolution publishBlogEntry() throws Throwable {
        return handleBusinessLogicException(new BusinessLogicExecution(){
            public Resolution execute() throws Throwable {
                blogService.publishBlogEntry(blogEntry);
                getContext().getMessages().add(new LocalizableMessage("foo.bar.blog.web.action.registered.BlogsProfileActionBean.messages.blogEntryPublished"));
                return new RedirectResolution(BlogsProfileActionBean.class, "editBlogEntry")
                            .addParameter("blog", blogEntry.getBlog().getId())
                            .addParameter("blogEntry", blogEntry.getId());
            }
        });
    }


    /**
     * Load page that display a blog entry details.
     * Input - blogEntry
     * Output - blogEntry
     * @return
     */
    @Profiled(tag="BlogsProfileActionBean_viewBlogEntry", logFailuresSeparately = true)
    public Resolution viewBlogEntry() {
        return new ForwardResolution(generateThemePath(VIEW_BLOG_ENTRY));
    }


    /**
     * Display page that allows editing of a particular blog entry.
     * Input - blogEntry
     * Output - blogEntry
     * @return
     */
    @Profiled(tag="BlogsProfileActionBean_editBlogEntry", logFailuresSeparately = true)
    public Resolution editBlogEntry() {
        return new ForwardResolution(generateThemePath(EDIT_BLOG_ENTRY));
    }


    /**
     * Delete a blog entry and then redirect to view all blog entries page
     * Input - blogEntry
     * Output - blog
     * @return
     */
    @Profiled(tag="BlogsProfileActionBean_deleteBlogEntry", logFailuresSeparately = true)
    public Resolution deleteBlogEntry() {
        blogService.deleteBlogEntry(blogEntry);
        getContext().getMessages().add(new LocalizableMessage("foo.bar.blog.web.action.registered.BlogsProfileActionBean.messages.blogEntryDeleted"));
        return new RedirectResolution(BlogsProfileActionBean.class, "viewBlogEntries")
                .addParameter("blog", blog.getId());
    }


    /**
     * (Ajax) Preview a blog entry.
     * Input - previewContent, entry
     * Output - result
     * @return
     */
    @Profiled(tag="BlogsProfileActionBean_previewBlogEntry", logFailuresSeparately = true)
    public Resolution previewBlogEntry() {
        BaseRenderContext context = new BaseRenderContext();
        context.setParameters(new LinkedHashMap(){
            {
                put(BlogRenderEngine.RENDER_CONTEXT_ENTRY_ID_PARAM_KEY, blogEntry.getId());
            }
        });
        final String result = renderEngine.render(previewContent == null ? "" : previewContent, context);
        return new HtmlResolution(result);
    }


    /**
     * Loads the page listing blog entry comments.
     * Input - blogEntry
     * Output - blogEntryComments
     * @return Resolution
     */
    @Profiled(tag="BlogsProfileActionBean_viewBlogEntryComments", logFailuresSeparately = true)
    public Resolution viewBlogEntryComments() {
        Set<Comment> comments = blogEntry.getComments();
        blogEntryComments = CollectionUtils.collect(comments, new DecoratedCommentTransformer(renderEngine));
        return new ForwardResolution(generateThemePath(VIEW_BLOG_ENTRY_COMMENTS));
    }


    /**
     * Delete a blog entry comment
     * Input - blogEntryComment
     * Output - blogEntry
     * @return
     */
    @Profiled(tag="BlogsProfileActionBean_deleteBlogEntryComment", logFailuresSeparately = true)
    public Resolution deleteBlogEntryComment() {
        blogService.deleteBlogEntryComment(blogEntryComment);
        getContext().getMessages().add(new LocalizableMessage("foo.bar.blog.web.action.registered.BlogsProfileActionBEan.messages.blogEntryCommentDeleted"));
        return new RedirectResolution(BlogsProfileActionBean.class, "viewBlogEntryComments")
                .addParameter("blogEntry", blogEntryComment.getEntry().getId());
    }


    /**
     * (Ajax) no resolution, make available blog's theme names for <stripes:actionBean .... /> usage.
     * Input - none
     * Output - allBlogThemeNames
     * @return
     * @throws URISyntaxException
     */
    @Profiled(tag="BlogsProfileActionBean_allBlogThemeNames", logFailuresSeparately = true)
    public Resolution allBlogThemeNames() throws URISyntaxException  {
        allBlogThemeNames = blogService.getAllBlogThemeNames();
        return new NullResolution();
    }



    private ExtendedPaginatedList members;
    private ExtendedPaginatedList nonMembers;
    @Validate(converter=UserTypeConverter.class, on="membershipAdd")
    private List<User> toBeAddedMembers;
    @Validate(converter=UserTypeConverter.class, on="membershipDelete")
    private List<User> toBeRemovedMembers;
    private String search;


    /**
     * Display a page has 2 tables, one contains members of this blog and another one containing
     * non-members of this blog.
     * Input - blog
     * Output - nonMembers, members
     * @return
     */
    @Profiled(tag="BlogsProfileActionBean_membership", logFailuresSeparately = true)
    public Resolution membership() {
        nonMembers = blogService.getBlogNonMembers(getPaginatedList("blogNonMembers"), blog, search);
        members = blogService.getBlogMembers(getPaginatedList("blogMembers"), blog);
        return new ForwardResolution(generateThemePath(MEMBERSHIP));
    }
    @After(stages={LifecycleStage.BindingAndValidation}, on="membership,membershipAdd,membsershipDelete")
    public void retrieve_membership_search() {  // used search stored, if there's one
        if (search == null || search.trim().length()<=0) {
            String searchStoredInSession = (String) getSessionAttribute("_search");
            if (searchStoredInSession != null && searchStoredInSession.trim().length()>0) {
                search = searchStoredInSession;
            }
        }
    }
    @After(stages={LifecycleStage.BindingAndValidation}, on="membershipSearch")
    public void store_membership_search() { // store search keyword
        if (search != null && search.trim().length()>0) {
            setSessionAttribute("_search", search);
        }
    }


    /**
     * (Ajax) Search for non-members of this blog and update the non-membership table.
     * Input - blog, search
     * Output - nonMembers
     * @return
     */
    @Profiled(tag="BlogsProfileActionBean_membershipSearch", logFailuresSeparately = true)
    public Resolution membershipSearch() {
        nonMembers = blogService.getBlogNonMembers(getPaginatedList("blogNonMembers"), blog, search);
        return new ForwardResolution(generateThemePath(NON_MEMBERSHIP_TABLE));
    }


    /**
     * Add member to a blog
     * Input - toBeAddedMembers, blog
     * Output - blog
     * @return
     */
    @Profiled(tag="BlogsProfileActionBean_membershipAdd", logFailuresSeparately = true)
    public Resolution membershipAdd() {
        blogService.addMembersToBlog(toBeAddedMembers, blog);
        return new RedirectResolution(BlogsProfileActionBean.class, "membership")
                .addParameter("blog", blog.getId());
    }


    /**
     * Delete a member from this blog
     * Input - toBeRemovedMembers, blog
     * Output - blog
     * @return
     */
    @Profiled(tag="BlogsProfileActionBean_membershipDelete", logFailuresSeparately = true)
    public Resolution membershipDelete() {
        blogService.removeMembersFromBlog(toBeRemovedMembers, blog);
        return new RedirectResolution(BlogsProfileActionBean.class, "membership")
                    .addParameter("blog", blog.getId());
    }

















    
    public ExtendedPaginatedList getMembers() {
        return members;
    }

    public void setMembers(ExtendedPaginatedList members) {
        this.members = members;
    }

    public ExtendedPaginatedList getNonMembers() {
        return nonMembers;
    }

    public void setNonMembers(ExtendedPaginatedList nonMembers) {
        this.nonMembers = nonMembers;
    }

    public List<User> getToBeAddedMembers() {
        return toBeAddedMembers;
    }

    public void setToBeAddedMembers(List<User> toBeAddedMembers) {
        this.toBeAddedMembers = toBeAddedMembers;
    }

    public List<User> getToBeRemovedMembers() {
        return toBeRemovedMembers;
    }

    public void setToBeRemovedMembers(List<User> toBeRemovedMembers) {
        this.toBeRemovedMembers = toBeRemovedMembers;
    }

    public String getSearch() {
        return search;
    }

    public void setSearch(String search) {
        this.search = search;
    }

    public List<String> getAllBlogThemeNames() {
        return allBlogThemeNames;
    }

    public void setAllBlogThemeNames(List<String> allBlogThemeNames) {
        this.allBlogThemeNames = allBlogThemeNames;
    }

    public Resolution links() {
        links = blogService.getLinks(blog, getPaginatedList("links"));
        return new ForwardResolution(generateThemePath(VIEW_LINKS));
    }

    public Resolution addLink() {
        return new ForwardResolution(generateThemePath(ADD_LINK));
    }

    public Resolution editLink() {
        return new ForwardResolution(generateThemePath(EDIT_LINK));
    }

    public Resolution deleteLink() {
        blogService.deleteLink(link);
        blog = link.getBlog();
        links = blogService.getLinks(link.getBlog(), getPaginatedList("links"));
        getContext().getMessages().add(new LocalizableMessage("foo.bar.blog.web.action.registered.BlogsProfileActionBean.messages.linkDeleted"));
        return new ForwardResolution(generateThemePath(VIEW_LINKS));
    }

    public Resolution updateLink() {
        blogService.updateLink(link);
        links = blogService.getLinks(link.getBlog(), getPaginatedList("links"));
        getContext().getMessages().add(new LocalizableMessage("foo.bar.blog.web.action.registered.BlogsProfileActionBean.messages.linkSaved"));
        setAjaxRequestSuccessHeader();
        return new ForwardResolution(generateThemePath(VIEW_LINKS_TABLE));
    }


    public Resolution saveLink() throws Throwable {
        return handleBusinessLogicException(new BusinessLogicExecution(){
            public Resolution execute() throws Throwable {
                link.setBlog(blog);
                blogService.saveLink(link);
                links = blogService.getLinks(link.getBlog(), getPaginatedList("links"));
                getContext().getMessages().add(new LocalizableMessage("foo.bar.blog.web.action.registered.BlogsProfileActionBean.messages.linkSaved"));
                setAjaxRequestSuccessHeader();
                return new ForwardResolution(generateThemePath(VIEW_LINKS_TABLE));
            }
        });
    }


    public Resolution tags() {
        tags = blogService.getTags(blog, getPaginatedList("tags"));
        return new ForwardResolution(generateThemePath(VIEW_TAGS));
    }

    public Resolution addTag() {
        return new ForwardResolution(generateThemePath(ADD_TAG));
    }

    public Resolution editTag() {
        return new ForwardResolution(generateThemePath(EDIT_TAG));
    }

    public Resolution deleteTag() {
                blogService.deleteTag(tag);
                blog = tag.getBlog();
                tags = blogService.getTags(tag.getBlog(), getPaginatedList("tags"));
                getContext().getMessages().add(new LocalizableMessage("foo.bar.blog.web.action.registered.BlogsProfileActionBean.messages.tagDeleted"));
                return new ForwardResolution(generateThemePath(VIEW_TAGS));
    }

    public Resolution updateTag() {
        blogService.updateTag(tag);
        tags = blogService.getTags(tag.getBlog(), getPaginatedList("tags"));
        getContext().getMessages().add(new LocalizableMessage("foo.bar.blog.web.action.registered.BlogsProfileActionBean.messages.tagSaved"));
        setAjaxRequestSuccessHeader();
        return new ForwardResolution(generateThemePath(VIEW_TAGS_TABLE));
    }

    public Resolution saveTag() throws Throwable {
        return handleBusinessLogicException(new BusinessLogicExecution(){
            public Resolution execute() throws Throwable {
                tag.setBlog(blog);
                blogService.saveTag(tag);
                tags = blogService.getTags(tag.getBlog(), getPaginatedList("tags"));
                getContext().getMessages().add(new LocalizableMessage("foo.bar.blog.web.action.registered.BlogsProfileActionBean.messages.tagSaved"));
                setAjaxRequestSuccessHeader();
                return new ForwardResolution(generateThemePath(VIEW_TAGS_TABLE));
            }
        });
    }


    public Resolution resources() {
        resources = blogService.getResources(blogEntry, getPaginatedList("resources"));
        return new ForwardResolution(generateThemePath(VIEW_RESOURCES));
    }

    public Resolution addResource() {
        return new ForwardResolution(generateThemePath(ADD_RESOURCE));
    }

    public Resolution editResource() {
        return new ForwardResolution(generateThemePath(EDIT_RESOURCE));
    }

    public Resolution deleteResource() {
        blogService.deleteResource(resource);
        blogEntry = resource.getEntry();
        resources = blogService.getResources(resource.getEntry(), getPaginatedList("resources"));
        getContext().getMessages().add(new LocalizableMessage("foo.bar.blog.web.action.registered.BlogsProfileActionBean.messages.resourceDeleted"));
        return new ForwardResolution(generateThemePath(VIEW_RESOURCES));
    }

    public Resolution updateResource() throws IOException {
        resource.setMimeType(resourceFile.getContentType());
        resource.setContent(IOUtils.toBytes(resourceFile.getInputStream()));
        blogService.updateResource(resource);
        blogEntry = resource.getEntry();
        resources = blogService.getResources(resource.getEntry(), getPaginatedList("resources"));
        getContext().getMessages().add(new LocalizableMessage("foo.bar.blog.web.action.registered.BlogsProfileActionBean.messages.resourceSaved"));
        resourceFile.delete();
        return new ForwardResolution(generateThemePath(VIEW_RESOURCES));
    }
    


    public Resolution saveResource() throws Throwable {
        return handleBusinessLogicException(new BusinessLogicExecution(){
            public Resolution execute() throws Throwable {
                resource.setEntry(blogEntry);
                resource.setMimeType(resourceFile.getContentType());
                resource.setContent(IOUtils.toBytes(resourceFile.getInputStream()));
                blogService.saveResource(resource);
                resourceFile.delete();
                getContext().getMessages().add(new LocalizableMessage("foo.bar.blog.web.action.registered.BlogsProfileActionBean.messages.resourceSaved"));
                return new RedirectResolution(BlogsProfileActionBean.class, "resources")
                        .addParameter("blogEntry", blogEntry.getId());
            }
        });
    }










    
    public Set<Comment> getBlogEntryComments() {
        return blogEntryComments;
    }

    public void setBlogEntryComments(Set<Comment> blogEntryComments) {
        this.blogEntryComments = blogEntryComments;
    }

    public Comment getBlogEntryComment() {
        return blogEntryComment;
    }

    public void setBlogEntryComment(Comment blogEntryComment) {
        this.blogEntryComment = blogEntryComment;
    }

    public ExtendedPaginatedList getBlogEntries() {
        return blogEntries;
    }

    public void setBlogEntries(ExtendedPaginatedList blogEntries) {
        this.blogEntries = blogEntries;
    }

    public Blog getBlog() {
        return blog;
    }

    public void setBlog(Blog blog) {
        this.blog = blog;
    }

    public void setEntries(ExtendedPaginatedList entries) {
        this.entries = entries;
    }

    public ExtendedPaginatedList getEntries() {
        return entries;
    }

    public ExtendedPaginatedList getOwnershipBlogs() {
        return ownershipBlogs;
    }

    public void setOwnershipBlogs(ExtendedPaginatedList ownershipBlogs) {
        this.ownershipBlogs = ownershipBlogs;
    }

    public ExtendedPaginatedList getMembershipBlogs() {
        return membershipBlogs;
    }

    public void setMembershipBlogs(ExtendedPaginatedList membershipBlogs) {
        this.membershipBlogs = membershipBlogs;
    }


    public ExtendedPaginatedList getAllBlogs() {
        return allBlogs;
    }

    public void setAllBlogs(ExtendedPaginatedList allBlogs) {
        this.allBlogs = allBlogs;
    }


    public Entry getBlogEntry() {
        return blogEntry;
    }

    public void setBlogEntry(Entry blogEntry) {
        this.blogEntry = blogEntry;
    }

    public String getPreviewContent() {
        return previewContent;
    }

    public void setPreviewContent(String previewContent) {
        this.previewContent = previewContent;
    }

    public ExtendedPaginatedList getLinks() {
        return links;
    }

    public void setLinks(ExtendedPaginatedList links) {
        this.links = links;
    }

    public ExtendedPaginatedList getTags() {
        return tags;
    }

    public void setTags(ExtendedPaginatedList tags) {
        this.tags = tags;
    }

    public ExtendedPaginatedList getResources() {
        return resources;
    }

    public void setResources(ExtendedPaginatedList resources) {
        this.resources = resources;
    }

    public Link getLink() {
        return link;
    }

    public void setLink(Link link) {
        this.link = link;
    }

    public Tag getTag() {
        return tag;
    }

    public void setTag(Tag tag) {
        this.tag = tag;
    }

    public Resource getResource() {
        return resource;
    }

    public void setResource(Resource resource) {
        this.resource = resource;
    }


    public FileBean getResourceFile() {
        return resourceFile;
    }

    public void setResourceFile(FileBean resourceFile) {
        this.resourceFile = resourceFile;
    }

    @SpringBean("blogService")
    public void setBlogService(BlogService blogService) {
        this.blogService = blogService;
    }

    @SpringBean("renderEngine")
    public void setRenderEngine(RenderEngine renderEngine) {
        this.renderEngine = renderEngine;
    }

}
